package com.lin.boot05webadmin.controller;

import com.lin.boot05webadmin.bean.Account;
import com.lin.boot05webadmin.bean.User;
import com.lin.boot05webadmin.service.AccountService;
import com.lin.boot05webadmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**主页面
 * @author ChenweiLin
 * @create 2021-04-02 21:15
 */
@Controller
public class IndexController {


    @Autowired
    AccountService accountService;
    /**
     * 来登录页
     * @return
     */
    //匹配两个路径
    @GetMapping(value = {"/","/login"})
    public String loginPage(){

        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session,Model model){

        if (!StringUtils.isEmpty(user.getUserName()) && StringUtils.hasLength(user.getPassWord())){
            //把登陆成功的用户保存起来
            session.setAttribute("loginUser", user);
            //把登录成功的重新定向到main.html; 重新定向防止表单重复提交
            return "redirect:main.html";
        }else {
            model.addAttribute("msg", "账号密码错误");
            return "login";
        }


        //登录成功重定向到main.html;
        //设置重定向以防止表单重复提交

    }

    @GetMapping("/main.html")
    public String mainPage(HttpSession session, Model model){

//        //是否登录。 拦截器，过滤器
//        Object loginUser = session.getAttribute("loginUser");
//        if (loginUser != null){
//            return "main";
//        }else {
//            //回到登陆页面
//            model.addAttribute("msg", "请重新登录");
//            return "login";
//        }
        return "main";

    }

    @ResponseBody //返回json数据
    @GetMapping("/acct")
    public Account getByID(@RequestParam("id") int id){

        return accountService.getAcct(id);
    }

//    @ResponseBody //返回json数据
//    @GetMapping("/acct1")
//    public Account getByMoney(@RequestParam("money") String money){
//
//        return accountService.getAcctByMoney(money);
//    }
}
